/*
 * PL011 UART driver
 *
 * Copyright (c) 2005-2014 ARM Ltd.  All Rights Reserved.
 */

#include <config.h>
#include "fvp_uart.h"

#ifdef BOARD_FVP_AEM_BASE

/*
 * UART instance: will be placed using the scatter file
 */
static struct pl011_uart uart;
static struct pl011_uart *port0 = (struct pl011_uart *)UART_BASE_ADDR;


void UartInit(void)
{
    /*
     * ensure the UART is disabled
     */
    uart.UARTCR  = 0x0;

    /*
     * OK, now program this thing up
     */
    uart.UARTECR   = 0x0;  // Clear the receive status (i.e. error) register
    uart.UARTLCR_H = 0x0 | PL011_LCR_WORD_LENGTH_8 | PL011_LCR_FIFO_DISABLE | \
                     PL011_LCR_ONE_STOP_BIT | PL011_LCR_PARITY_DISABLE | PL011_LCR_BREAK_DISABLE;

    uart.UARTIBRD = PL011_IBRD_DIV_38400;
    uart.UARTFBRD = PL011_FBRD_DIV_38400;

    /*
     * mask and clear all interrupts
     */
    uart.UARTIMSC = 0x0;
    uart.UARTICR  = PL011_ICR_CLR_ALL_IRQS;

    uart.UARTCR  = 0x0 | PL011_CR_UART_ENABLE | PL011_CR_TX_ENABLE | PL011_CR_RX_ENABLE;

    port0->UARTCR = uart.UARTCR;
    port0->UARTECR = uart.UARTECR;
    port0->UARTLCR_H = uart.UARTLCR_H;
    port0->UARTIBRD = uart.UARTIBRD;
    port0->UARTFBRD = uart.UARTFBRD;
    port0->UARTIMSC = uart.UARTIMSC;
    port0->UARTICR = uart.UARTICR;
    port0->UARTCR = uart.UARTCR;
    return;
}

// void uart_putc_polled(char c)
// {
//     /* Wait for UART to become free */
//     /* Note that FIFOs are not being used here */
//     while (port0->UARTFR & PL011_FR_BUSY_FLAG);

//     /* Write character and send it */
//     port0->UARTDR = c;
// }

// char uart_getchar_polled(void)
// {
//     /* Wait for UART to become free */
//     /* Note that FIFOs are not being used here */
//     while (port0->UARTFR & PL011_FR_BUSY_FLAG);
//     /* Read character received */
//     return port0->UARTDR;
// }


#else
#define UartInit()
#endif